﻿using System;
using System.Linq;
using System.Windows.Forms;
using CommonLibrary;
using CommonLibrary.Models.Profiles;

namespace InputManager.InputHandlers
{
    class SpecialProcessor
    {
        private static IAppContext _context;

        public SpecialProcessor(IAppContext context)
        {
            _context = context;
        }

        public bool ValidateInput(InputType inputType, object inputData)
        {
            switch (inputType)
            {
                // Todo add suport for key up
                case InputType.KeyDown:
                    return ProcessKey(inputData);
                case InputType.MouseDown:
                    return ProcessMouseButton(inputData);
                case InputType.MouseMove:
                    return ProcessMouseMove(inputData);
                case InputType.MouseWheel:
                    return ProcessMouseWheel(inputData);
            }
            return false;
        }

        private bool ProcessMouseButton(object inputData)
        {
            var action = _context.ProfileConfig.SpecialActions.FirstOrDefault(d => d.Key.Equals("M_"+inputData.ToString())).Value;
            if (action == null) return false;

            return ExecuteSpecialAction(action);
        }

        private bool ProcessMouseMove(object inputData)
        {
            return false;
        }

        private bool ProcessMouseWheel(object inputData)
        {
            return false;
        }

        private bool ProcessKey(object inputData)
        {
            var key = Enum.ToObject(typeof (Keys), inputData);
            var action = _context.ProfileConfig.SpecialActions.FirstOrDefault(d => d.Key.Equals(key.ToString())).Value;
            if (action == null) return false;

            return ExecuteSpecialAction(action);
        }

        private bool ExecuteSpecialAction(SepcialAction action)
        {
            switch (action.ActionName)
            {
                case "ChangeProfile":
                    _context.GetApplicationProfile("", Convert.ToInt16(action.Parameter));
                    return false;
            }
            return false;
        }
    }
}
